import tkinter as tk
from orna import check_right,winner,show_word,game_word
from tkinter import messagebox

class Wordle:
    def __init__(self,word,root,settings):
        self.root = root
        self.word = word
        self.settings = settings
        self.answer = [["?",0] for i in range(len(self.word))]
        # 错误与生命总和恒定
        self.wrong = 0
        self.life = settings.difficulty*len(self.word)
        # 显示生命
        self.life_label = tk.Label(root,font=("Arial",15),
                                   width=50,
                                   fg="#4400FF",
                                   text=f"生命值剩余:💚×{self.life} 累计犯错:❌×{self.wrong}")
        # 输入框与字母deul
        self.entry = tk.Entry(root,bg="pink",fg="magenta",font=("Arial",24),
                              width=30)
        self.letters = [
            tk.Label(root, width=9, height=5, font=("fantasy", 24)
                     , bg=settings.color_list[i % 2])
            for i in range(len(word))]
        # 错误标签
        self.wrong_label = tk.Label(root,width=25,fg="red",font=("Arial",20))
        self.flag = 1

    def init_button(self):
        tk.Button(self.root,text="提交单词",bg="#66FFFF",
                  font=("fantasy",20),
                  command=self.hwa,height=3).place(x=200,y=720)
        tk.Button(self.root,text="重新开始",bg="#66FFFF",
                  font=("fantasy",20),
                  command=self.tasi,height=3).place(x=520,y=720)
        tk.Button(self.root, text="退出游戏", bg="#66FFFF",
                  font=("fantasy", 20),
                  command=self.cui, height=3).place(x=840, y=720)

    def init_widget(self):
        self.entry.place(x=330,y=620)
        self.life_label.place(x=325,y=320)
        self.wrong_label.place(x=400,y=550)
        for i in range(len(self.word)):
            self.letters[i].place(x=178*i+60,y=100)

        the_word = tk.Label(self.root,text="下面这个是单词显示区",fg="#222299",height=1
                            ,font=("Arial",15))
        the_word.place(x=500,y=50)
        # 显示单词了
        show_word(self.letters,self.answer,self.settings,self.flag)

    def hwa(self):
        if self.flag:
            self.flag = 0
        # 防止上来就报错
        if self.flag == 0:
            # 构建映射方便更新状态
            temp_s = self.entry.get().strip()
            self.entry.delete(0,100)
            self.answer = [[i,0] for i in temp_s]
            temp_i = check_right(self.answer,temp_s,right_word=self.word)

            # 游戏结束，公布单词
            if temp_i >= self.life:
                for i in range(len(temp_s)):
                    self.letters[i].config(text=temp_s[i])

                for i in range(len(self.word)):
                    self.letters[i].config(text=self.word[i])

                tk.messagebox.showinfo("失败","很遗憾，您失败了")
                self.tasi()

            # 判断单词正确性
            if temp_i == -1:
                winner(self.root,self.letters)
            elif temp_i == 0:
                self.wrong_label.config(text="请输入正确格式和长度的单词！")
                return
            else:
                self.wrong += temp_i
                self.life -= temp_i
                self.life_label.config(text=f"生命值剩余:💚×{self.life} 累计犯错:❌×{self.wrong} ")
            # 更新单词状态
            show_word(self.letters, self.answer, self.settings, self.flag)


    def cui(self):
        if messagebox.askyesno("退出游戏","确认退出游戏?"):
            self.root.quit()

    def tasi(self):
        if tk.messagebox.askyesno("重新开始","确认重新开始?"):
            self.word = game_word()
            self.answer = [["?", 0] for i in range(len(self.word))]
            # 错误与生命总和恒定
            self.wrong = 0
            self.life = self.settings.difficulty * len(self.word)
            # 显示生命
            self.life_label.config(text=f"生命值剩余:💚×{self.life} 累计犯错:❌×{self.wrong}")
            # 重置单词显示
            self.letters.clear()
            self.letters = [
                tk.Label(self.root, width=9, height=5, font=("fantasy", 24)
                         , bg=self.settings.color_list[i % 2])
                for i in range(len(self.word))]
            # 错误标签
            self.wrong_label.config(text="")
            # ##########################
            self.flag = 1
            self.init_widget()
            self.init_button()
